﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VietKinsNutrition.Common;
using VietKinsNutrition.UserControl;
using VietKinsNutrition.Data;

namespace VietKinsNutrition
{
    public partial class MainForm : DevExpress.XtraEditors.XtraForm
    {
        AddTab addTab = new AddTab();
        bool checkDb = false;
        public MainForm()
        {
            InitializeComponent();
            checkDb = StoreDatabaseInfor.Readfile();
            if(!checkDb)
            {
                frmChooseDB choiceDb = new frmChooseDB();
                choiceDb.ShowDialog();
            }
            else if(!ClsConnection.CheckDb())
            {
                frmChooseDB choiceDb = new frmChooseDB();
                choiceDb.ShowDialog();
            }
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {

        }

        private void nbiThucpham_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (!ClsConnection.CheckDb())
            {
                frmChooseDB choiceDb = new frmChooseDB();
                choiceDb.ShowDialog();
            }
            else
            {
                checkAndTab("Thư viện thực phẩm", new Thuvienthucpham());
            }
        }

        public void checkAndTab(string Caption, XtraUserControl UserColtrol)
        {
            bool temp = false;
            foreach (DevExpress.XtraTab.XtraTabPage tab in this.xtraTabControl.TabPages)
            {
                if (tab.Text == Caption)
                {
                    temp = true;
                }
            }
            if (temp)
            {
            }
            else
            {
                addTab.Add(this.xtraTabControl, Caption, UserColtrol);
            }
        }

        public bool checkConnec()
        {
            if (!ClsConnection.CheckDb())
            {
                frmChooseDB choiceDb = new frmChooseDB();
                choiceDb.ShowDialog();
                return false;
            }
            return true;
        }

        private void xtraTabControl_CloseButtonClick(object sender, EventArgs e)
        {
            this.xtraTabControl.TabPages.RemoveAt(this.xtraTabControl.TabPages.TabControl.SelectedTabPageIndex);
        }

        private void bsiOpen_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChooseDB frmDB = new frmChooseDB();
            frmDB.ShowDialog();
        }

        private void nbiMonan_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (!ClsConnection.CheckDb())
            {
                frmChooseDB choiceDb = new frmChooseDB();
                choiceDb.ShowDialog();
            }
            else
            {
                checkAndTab("Thư viện món ăn", new Thuvienmonan());
            }
        }

        private void nbiChebien_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (checkConnec())
            {
                ThuVien.FrmTblDm_Loaimon frmLoaimon = new ThuVien.FrmTblDm_Loaimon();
                frmLoaimon.ShowDialog();
            }
        }

        private void nbiNhapluukho_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (checkConnec())
            {
                ThucPham.frmNhapkho frmNhapKho = new ThucPham.frmNhapkho();
                frmNhapKho.ShowDialog();
            }
        }    
    }
}